package com.example.afinal;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.fragment.app.Fragment;

import com.example.afinal.Aboutme.AboutActivity;
import com.example.afinal.Aboutme.ChangePwdActivity;
import com.example.afinal.Aboutme.LoginActivity;
import com.example.afinal.bean.User;


public class MyActivity extends Fragment implements View.OnClickListener {
    private LinearLayout layXiugai;
    private LinearLayout layGuanyu;
    private Button btnEtc;
    View view;
    private TextView tvUsername;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.activity_my, container, false);
        initView();
        if (User.name == null) {
            tvUsername.setText("请登录");
            btnEtc.setText("登录");
            btnEtc.setBackgroundColor(Color.parseColor("#2196F3"));
        } else {
            btnEtc.setText("退出登录");
            btnEtc.setBackgroundColor(Color.parseColor("#bfbfbf"));
            tvUsername.setText(User.name);
        }
        return view;
    }

    public void initView() {
        layXiugai = (LinearLayout) view.findViewById(R.id.lay_xiugai);
        layGuanyu = (LinearLayout) view.findViewById(R.id.lay_guanyu);
        tvUsername = (TextView) view.findViewById(R.id.tv_username);
        btnEtc = (Button) view.findViewById(R.id.btn_etc);
        layGuanyu.setOnClickListener(this);
        layXiugai.setOnClickListener(this);
        btnEtc.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_etc:
                if (User.loginstate==false) {
                    Intent intentll = new Intent(getContext(), LoginActivity.class);
                    startActivity(intentll);
                } else {
                    User.name = null;
                    User.pwd = null;
                    tvUsername.setText("请登录");
                    btnEtc.setText("登录");
                    btnEtc.setBackgroundColor(Color.parseColor("#2196F3"));
                    User.loginstate = !User.loginstate;
                }
                break;
            case R.id.lay_guanyu:
                Intent intentss = new Intent(getContext(), AboutActivity.class);
                startActivity(intentss);
                break;
            case R.id.lay_xiugai:
                if (User.loginstate==false){
                    Toast.makeText(getContext(),"请先登录！",Toast.LENGTH_SHORT).show();
                }else {
                    Intent intentkk = new Intent(getContext(), ChangePwdActivity.class);
                    startActivity(intentkk);
                }
                break;
        }
    }
}
